package down.xiaoheihe.work.Controller;

import down.xiaoheihe.work.Entity.Msghistory;
import down.xiaoheihe.work.Entity.MsghistoryExample;
import down.xiaoheihe.work.Entity.User;
import down.xiaoheihe.work.Mapper.*;
import down.xiaoheihe.work.Response.liaotianR;
import down.xiaoheihe.work.Service.service3;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@MapperScan("down.xiaoheihe.work.Mapper")
@CrossOrigin
public class liaotian {
    @Autowired
    UserMapper tUserMapper;
    @Autowired
    MsghistoryMapper hisMapper;
    @GetMapping("/liaotiantotal")
    public List<liaotianR> 查询我的所有聊天 () {
        List<liaotianR> result = new ArrayList<>();
        Subject subject = SecurityUtils.getSubject();
        Integer id = (Integer) subject.getPrincipal();
        if (id == null) {
            return null;
        }
        MsghistoryExample msghistoryExample = new MsghistoryExample();
        msghistoryExample.createCriteria().andFromidEqualTo(id);
        List<Msghistory> msghistories = hisMapper.selectByExample(msghistoryExample);
        for (Msghistory msghistory : msghistories) {
            liaotianR lR= new liaotianR();
            User tUser = tUserMapper.selectByPrimaryKey(msghistory.getToid());
            lR.setToid(msghistory.getToid());
            lR.setFromid(msghistory.getFromid());
            lR.setName(tUser.getUsername());
            lR.setImage(tUser.getImage());
            result.add(lR);
        }
        return result;
    }
}
